package com.kistring.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

public class AccelerometerDB {
	private static final String DATABASE_NAME = "tshek";
	private static final int DATABASE_VERSION = 1;
	private static final String TABLE_NAME = "ACCELEROMETER";
	private static final long TABLE_MAX_SIZE = 1000;

	private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS "
			+ TABLE_NAME + " (" + "key INTEGER PRIMARY KEY AUTOINCREMENT,"
			+ "gx DOUBLE," + "gy DOUBLE,"
			+ "gz DOUBLE," + "timestamp LONG" + ")";

	private Context context;
	private SQLiteDatabase db;

	public AccelerometerDB(Context context) {
		this.context = context;
		AccelerometerHelper accelerometerHelper = new AccelerometerHelper(
				this.context);
		db = accelerometerHelper.getWritableDatabase();
	}

	public long insert(float xx, float yy, float zz, long ts) {
		long rr = 0;
		db.beginTransaction();
		try {
			SQLiteStatement insertStmt;
		/*insertStmt = this.db.compileStatement("INSERT INTO " + TABLE_NAME
					+ " (gx,gy,gz,ts) VALUES ("+xx+","+yy+","+zz+","+ts+")");*/
			insertStmt = db.compileStatement( "INSERT INTO " + TABLE_NAME
					+ " (gx, gy, gz, ts) VALUES (?, ?, ?, ?)");
					/*insertStmt.bindDouble(1, xx);
			insertStmt.bindDouble(2, yy);
			insertStmt.bindDouble(3, zz);
			insertStmt.bindLong(4, ts);
*/
		ContentValues contentValues = new ContentValues();
		contentValues.put("gx", xx);
		contentValues.put("gy", yy);
		contentValues.put("gz", zz);
		contentValues.put("timestamp", ts);
		Log.i("DB", "Insert data");
		return db.insert(TABLE_NAME, null, contentValues);//insertStmt.executeInsert();
	}

	public void deleteAll() {
		db.delete(TABLE_NAME, null, null);
	}

	public List<String> selectAll() {
		List<String> list = new ArrayList<String>();
		Cursor cursor = this.db.query(TABLE_NAME, new String[] { "name" },
				null, null, null, null, "name desc");
		if (cursor.moveToFirst()) {
			do {
				list.add(cursor.getString(0));
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return list;
	}

	/* */
	private static class AccelerometerHelper extends SQLiteOpenHelper {

		public AccelerometerHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(CREATE_TABLE);
			Log.i("DB", "Database Accelerometer is created." + db.toString());
			db.setMaximumSize(TABLE_MAX_SIZE);
			
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("drop table if exists " + TABLE_NAME);
			Log.i("DB", "Database Accelerometer is upgraded." + db.toString());
		}

	}
}
